using System;
using System.Collections.Generic;
using System.Text;

namespace jsonReader 
{
    public class JsonObject : IJsonValue 
    {
        //public JsonObject() {

        //}
        private string name;

        private JsonPairCollection jsonPairs;

        public JsonObject(string name, JsonPairCollection jsonPairs) {
            if (string.IsNullOrEmpty(name)) {
                throw new ArgumentNullException("name");
            }

            this.name = name;
            this.jsonPairs = jsonPairs;
        }

        public string Name {
            get { return name; }
            set { name = value; }
        }

        public JsonPairCollection JsonPairs {
            get {
                return jsonPairs;
            }
        }

        /// <returns>A <see cref="T:System.String"></see> that represents the current <see cref="T:System.Object"></see>.</returns>
        public override string ToString() {
            return ToJsonString();
        }

        #region IJsonValue Members


        public string ToJsonString() {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("{");

            foreach (JsonPair pair in this.jsonPairs) {
                sb.AppendFormat("   {0} : {1}, ", pair.Name, pair.Value).AppendLine();
            }

            if (4 < sb.Length) {
                sb.Remove(sb.Length - 4, 4);
            }

            sb.AppendLine("}");

            return sb.ToString();
        }

        #endregion
    }
}
